package com.ktjiaoyu.server.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2 // 通过@EnableSwagger2注解来启用Swagger2
public class Swagger2Config {

    // Docket可以理解为API的正文
    @Bean
    public Docket createRestApi() { // 通过createRestApi()方法创建Docket的bean实例对象
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//appInfo()用来创建Api的基本信息(这些信息会展示在Swagger文档页面中)
                .select()
                // 下面的包名为oa-server项目controller所在包
                .apis(RequestHandlerSelectors.basePackage("com.ktjiaoyu.server.controller"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("源梦科技OA系统接口文档")
                .description("")
                .contact(new Contact("jjx", "http://localhost:8081", "1823992421@qq.com"))
                .version("1.0")
                .build();
    }

    private List<ApiKey> securitySchemes() {
        // 设置请求头信息
        List<ApiKey> result = new ArrayList<ApiKey>();
        // 参数一name为参数名，参数二keyname为参数key，参数三passAs为参数存放位置，其值有header、query、path等，其中header为请求头，query为请求参数，path为请求路径上的参数
        ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
        result.add(apiKey);
        return result;
    }

    private List<SecurityContext> securityContexts() {
        // 设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        // 调用下面定义的getContextByPath()，设置需要登录认证的路径，比如此处的/hello/.*
        result.add(getContextByPath("/hello/.*"));
        // 上面配置后，在Swagger文档中访问/hello路径时，需要用到securitySchemes()方法中配置的全局参数
        return result;
    }

    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    // defaultAuth() 配置Swagger2默认的认证行为
    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<SecurityReference>();
        // AuthorizationScope权限范围对象，其参数一scope为权限范围名称，global表示全局，其参数二为范围的描述(但凡某个请求的请求头添加到这个全局参数，后面在Swagger接口文档发送的其他请求都会默认添加上这个全局参数。)
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization",authorizationScopes));
        return result;
    }



}

